<?php
/**
 * Created by PhpStorm.
 * User: keyth
 * Date: 18-4-10
 * Time: 下午2:47
 * des:登录模块
 */

namespace app\index\controller;
use app\model\Student;
use app\model\Teacher;
use app\model\Admin;
use think\Controller;
use think\Cookie;
use think\Request;

class LoginController extends Controller
{
    protected $beforeActionList = [
        'is_login' =>  ['only'=>'index,login'],
  ];

    public function is_login()
    {
        $UR_Logstate= cookie('UR_Logstate');
        $Con=self::who();

        if ($Con->where('cookie_state', $UR_Logstate)->find() && $UR_Logstate!='') {
            $this->error(' 请不要重复登录！', url('/index/admin'));
        }
    }

    public function index(){
        return $this->fetch();
    }
    public function register(){
        return $this->fetch();
    }
    public function register_c(){
        $postdata=Request::instance()->post();
        if($postdata['is_tea']==1){
            $Con=new Teacher();
        }else{
            $Con=new Student();
        }
        if($Con->where('email',$postdata['email'])->select()){
            return $this->error('您的邮箱已经注册过了!','/index/login');
        }
        $Con->address=$postdata['address'];
        $Con->sex=$postdata['sex'];
        $Con->age=$postdata['age'];
        $Con->email=$postdata['email'];
        $Con->mobile=$postdata['mobile'];
        $Con->username=$postdata['username'];

        $Con->password=encrypt_pass($postdata['email'],$postdata['password']);
        if($Con->save()){

            return $this->success('账户注册成功!','/index/login');
        }
        return $this->error('系统繁忙!个人信息注册失败,请稍后再试','/index/login');
    }
    /**
     * leval:0-管理员 1-教师 2-学生
    **/
    public function login(){
        $postdata=Request::instance()->post();
        if($postdata['leval']==0){
            $Con=new Admin();
            $UR_distinctid=base64_encode(base64_encode('Admin'));
        }elseif($postdata['leval']==1){
            $Con=NEW Teacher();
            $UR_distinctid=base64_encode(base64_encode('Teacher'));
        }else{
            $Con=NEW Student();
            $UR_distinctid=base64_encode(base64_encode('Student'));
        }

        if($user=$Con->where('email',$postdata['email'])->where('password',encrypt_pass($postdata['email'],$postdata['password']))->select()){
            Cookie::set('UR_distinctid',$UR_distinctid,43200);
            $UR_Logstate=md5(sha1($UR_distinctid.time().$postdata['email']));
            Cookie::set('UR_Logstate',$UR_Logstate,43200);
            $user[0]->cookie_state=$UR_Logstate;
            $user[0]->save();
            return alert($user[0]->username.',登陆成功','/index/admin',6,3);
        }
        return alert('邮箱或密码错误,请重试!','/index/login',5,3);




    }
    /**
     * return 0--注销失败没有登录  1--注销成功
    **/
    static public function logout(){
        $Con=self::who();
        if($user=$Con->where('cookie_state',cookie('UR_Logstate'))->select()){
            $user[0]->cookie_state='';
            $user[0]->save();
            cookie('UR_Logstate', Null);
            cookie('UR_distinctid', Null);
            return alert('注销成功!','/index/login',6,3);
        }else{
            cookie('UR_Logstate', Null);
            cookie('UR_distinctid', Null);
            return alert('注销失败未登录!','/index/login',5,3);
        }


    }

    private static function who(){
        $UR_distinctid = base64_decode(base64_decode(cookie('UR_distinctid')));
        if($UR_distinctid=='Admin'){
            $Con=new Admin();
        }elseif($UR_distinctid=='Teacher'){
            $Con=new Teacher();
        }else{
            $Con=new Student();
        }
        return $Con;
    }

}